package com.smalls.newvideotwo.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

import com.smalls.newvideotwo.utils.Constant;

public class NetworkConnectChangedReceiver extends BroadcastReceiver {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "NetworkConnectChangedReceiver":"NetworkConnectChangedReceiver";
    private onNetworkConnectChangedListener onNetworkConnectChangedListener;

    public interface WifiState {
        public static final int WIFI_STATE_DISCONNECT = -200;
        public static final int WIFI_STATE_LEVEL_1 = 1;
        public static final int WIFI_STATE_LEVEL_2 = 2;
        public static final int WIFI_STATE_LEVEL_3 = 3;
        public static final int WIFI_STATE_LEVEL_4 = 4;
    }

    public interface onNetworkConnectChangedListener {
        void onEtherNetConnectChanged(NetworkInfo networkInfo);

        void onWifiConnectChanged(int i);
    }

    public void onReceive(Context context, Intent intent) {
        if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {
            int wifiState;
            WifiManager wifiManager = (WifiManager) context.getSystemService("wifi");
            NetworkInfo lanNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getNetworkInfo(9);
            int rssi = wifiManager.getConnectionInfo().getRssi();
            if (rssi == WifiState.WIFI_STATE_DISCONNECT) {
                wifiState = WifiState.WIFI_STATE_DISCONNECT;
            } else if (rssi <= 0 && rssi >= -50) {
                wifiState = 4;
            } else if (rssi < -50 && rssi >= -70) {
                wifiState = 3;
            } else if (rssi < -70 && rssi >= -80) {
                wifiState = 2;
            } else if (rssi >= -80 || rssi < -100) {
                wifiState = WifiState.WIFI_STATE_DISCONNECT;
            } else {
                wifiState = 1;
            }
            if (this.onNetworkConnectChangedListener != null) {
                this.onNetworkConnectChangedListener.onEtherNetConnectChanged(lanNetworkInfo);
                this.onNetworkConnectChangedListener.onWifiConnectChanged(wifiState);
            }
        }
    }

    public void setOnNetworkConnectChangedListener(onNetworkConnectChangedListener onNetworkConnectChangedListener) {
        this.onNetworkConnectChangedListener = onNetworkConnectChangedListener;
    }
}
